<?php namespace Home\Controller; 

//use Hdphp\Controller\Controller;

class UserCenterController extends CommonController{
//	地址表
	private $addrModel;
	private $orderModel;
	private $orderlistModel;
	private $userModel;
	private $userInforModel;
//	构造函数
	public function __init(){
//		1.判断用户是否登录
		parent::__init();
		$this->addrModel = new \Common\Model\Address;
		$this->orderModel = new \Common\Model\Orders;
		$this->orderlistModel = new \Common\Model\OrderList;
		$this->userModel = new \Common\Model\User;
		$this->userInforModel = new \Common\Model\UserInfor;
	}
	
//	1.用户中心首页
	public function index(){
		
		View::make();
	}
//	2.我的收货地址
	public function address(){
		if(IS_POST){
			if(!$this->addrModel->store()) View::error($this->addrModel->getError());
			View::success('新增成功！');
		}
//		获得用户id
		$uid = $_SESSION['uid'];
//		查询用户地址数据
		$addrData = $this->addrModel->where("user_uid = $uid")->get();
		View::with('addrData',$addrData);
		View::make();
	}
//	3.删除地址
	public function addrDel(){
		if(!$this->addrModel->addrDel()) View::error($this->addrModel->getError());
		View::success('删除成功！');
	}
//	4.异步获取地址
	public function getAddr(){
		$adid = Q('post.adid',0,'intval');
		$addrData = $this->addrModel->where("adid = $adid")->find();
		echo json_encode($addrData);
	}
	
//	5.修改地址
	public function editAddr(){
		if(IS_POST){
			if(!$this->addrModel->edit()) View::error($this->addrModel->getError());
			View::success('修改成功！');
		}
	}
	
//	6.我的订单
	public function myOrder(){
		$orderData = $this->orderModel->get();
		View::with('orderData',$orderData);
		View::make();
	}
//	7.异步删除订单
	public function orderDel(){
		$oid = Q('post.oid',0,'intval');
		$this->orderModel->where("oid = {$oid}")->delete();
		$this->orderlistModel->where("orders_oid = {$oid}")->delete();
		echo json_encode(array('status'=>true));
		die;
	}
	
//	8.安全设置
	public function safeSet(){
		
		View::make();
	}
//	9.修改密码
	public function changePwd(){
		if(IS_POST){
			if(!$this->userModel->changePwd()) View::error($this->userModel->getError());
			View::success('修改成功','safeSet');
		}
		View::make();
	}
	
//	10.用户信息
	public function userInfor(){
		$uid = $_SESSION['uid'];
		if(IS_POST){
			if(!$this->userInforModel->store()) View::error($this->userInforModel->getError());
			View::success('保存成功！','userInfor');
		}
		$inforData = $this->userModel->join('userinfor','uid','=','user_uid')->where("uid = {$uid}")->find();
		View::with('inforData',$inforData);
		View::make();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
